package cn.xorange.commons.security.entity;

import cn.hutool.core.date.DateUtil;
import cn.xorange.commons.utils.http.ServletUtils;
import cn.xorange.commons.utils.ip.AddressUtils;
import cn.xorange.commons.utils.ip.IpUtils;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.Data;
import lombok.Getter;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author : yangjian
 * @date : 2021/8/9
 */
@Data
public class LoginLog {

    public LoginLog(String loginName,String loginType, Integer status, String msg){
        HttpServletRequest request = ServletUtils.getRequest();
        String uaStr = request.getHeader("User-Agent");
        String ip = IpUtils.getIpAddr(request);

        UserAgent ua = UserAgent.parseUserAgentString(uaStr);
        String browser = ua.getBrowser().getName();
        String os = ua.getOperatingSystem().getName();
        String loginLocation = AddressUtils.getRealAddressByIP(ip);
        this.loginTime = DateUtil.date();
        this.loginName = loginName;
        this.loginType = loginType;
        this.browser = browser;
        this.os = os;
        this.ip = ip;
        this.location = loginLocation;
        this.status = String.valueOf(status);
        this.msg = msg;
    }

    private Integer id;
    @Getter
    private String loginName;
    @Getter
    private String loginType;
    @Getter
    private Long tenantId;
    @Getter
    private String tenantName;
    @Getter
    private Long userId;
    @Getter
    private String userName;
    @Getter
    private String ip;
    @Getter
    private String location;
    @Getter
    private String browser;
    @Getter
    private String os;
    @Getter
    private String status;
    @Getter
    private String msg;
    @Getter
    private Date loginTime;


    public void setTenantId(Long tenantId) {
        this.tenantId = tenantId;
    }

    public void setTenantName(String tenantName) {
        this.tenantName = tenantName;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

}
